from urllib import request
from config import config_qywx as config
from utils import logging
from api import api_qywx_common as common
import sys, json, logging

#发送文本消息
def send_weather_info(QyWxInfo):
    access_token_dictionary = common.get_weather_access_token()
    if access_token_dictionary['errcode'] != 0:
        logging.error(access_token_dictionary)
        return

    QYWX_SEND_INFO_URL = config.QYWX_SEND_INFO_URL
    QYWX_SEND_INFO_URL = str(QYWX_SEND_INFO_URL).replace("${access_token}", access_token_dictionary["access_token"])

    headers = {'Content-Type': 'application/json'}
    req2 = request.Request(QYWX_SEND_INFO_URL,
                           headers=headers,
                           data=bytes(json.dumps(get_send_info(QyWxInfo)),
                           encoding="UTF-8"))
    try:
        resp2 = request.urlopen(req2)
    except request.HTTPError as e:
        logging.exception(e.msg, e.args)
        sys.exit(1)

    send_info_bytes = resp2.read()
    send_info_str = json.dumps(str(send_info_bytes, "UTF-8"))
    logging.info(send_info_str)


#发送文本消息
def send_news_info(QyWxInfo):
    access_token_dictionary = common.get_news_access_token()
    if access_token_dictionary['errcode'] != 0:
        logging.error(access_token_dictionary)
        return

    QYWX_SEND_INFO_URL = config.QYWX_SEND_INFO_URL
    QYWX_SEND_INFO_URL = str(QYWX_SEND_INFO_URL).replace("${access_token}", access_token_dictionary["access_token"])

    headers = {'Content-Type': 'application/json'}
    req2 = request.Request(QYWX_SEND_INFO_URL,
                           headers=headers,
                           data=bytes(json.dumps(get_send_info(QyWxInfo)),
                           encoding="UTF-8"))
    try:
        resp2 = request.urlopen(req2)
    except request.HTTPError as e:
        logging.exception(e.msg, e.args)
        sys.exit(1)

    send_info_bytes = resp2.read()
    send_info_str = json.dumps(str(send_info_bytes, "UTF-8"))
    logging.info(send_info_str)


def get_send_info(QyWxInfo):
    data = {}
    if QyWxInfo.msgtype == "text":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "text": {
                "content": QyWxInfo.content
            },
            "safe": QyWxInfo.safe
        }
    elif QyWxInfo.msgtype == "image":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "image": {
                "media": QyWxInfo.mediaid
            },
            "safe": QyWxInfo.safe
        }
    elif QyWxInfo.msgtype == "voice":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "voice": {
                "media": QyWxInfo.mediaid
            }
        }
    elif QyWxInfo.msgtype == "video":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "voice": {
                "media_id": QyWxInfo.mediaid,
                "title": QyWxInfo.title,
                "description": QyWxInfo.description
            },
            "safe": QyWxInfo.safe
        }
    elif QyWxInfo.msgtype == "file":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "file": {
                "media_id": QyWxInfo.mediaid
            },
            "safe": QyWxInfo.safe
        }
    elif QyWxInfo.msgtype == "textcard":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "textcard": {
                "title": QyWxInfo.title,
                "description": QyWxInfo.description,
                "url": QyWxInfo.url,
                "btntxt": QyWxInfo.btntxt
            }
        }
    elif QyWxInfo.msgtype == "news":
        data = {
            "touser": QyWxInfo.touser,
            "msgtype": QyWxInfo.msgtype,
            "agentid": QyWxInfo.agentid,
            "news": {
                "articles" : QyWxInfo.articles
            }
        }
    return data


